#include "page_mode_add.h"
#include "page_base.h"
#include "wind_mgr.h"
#include "tools.h"
#include "R.h"
#include "conf_mgr.h"

PageModeAdd::PageModeAdd(ViewGroup *mWindPageBox, int actionType, int position, pageType type)
: PageBase(mWindPageBox, "@layout/page_water_mode_add", type){

    thisLayout = this->dPageLayout;
    dataPos = position;
    LOGE("position===================%d", position);
    LOGE("type===================%d", actionType);
    // changeModeData.dataType = nullptr;
    // changeModeData.imageDir = nullptr;
    // changeModeData.modeName = nullptr;
    // changeModeData.temperature = nullptr;
    // changeModeData.chuShuiLiang = -1;
    // changeModeData.canDelete = -1;

    loadPageInfo();

    startPageAction(type, position);
    
    setOnClickListenerMethod();
}

void PageModeAdd::startPageAction(pageType type, int position){
    dataPos = position;

    if (type == PAGE_WATER_MODE_ADD)
    {
        changeModeData.imageDir = "@mipmap/pic_chushui_lengshui";
        changeModeData.dataType = "addNew";
        changeModeData.modeName = modeName->getText();
        changeModeData.canDelete = 1;
        changeModeData.temperature = std::string("55");
        changeModeData.chuShuiLiang = 500;
        deleteMode->setVisibility(View::GONE);
    } else if (type == PAGE_WATER_MODE_MODIFY) {
        modeDataList = d_fileLoad->getModeDataList();
        changeModeData = modeDataList.at(dataPos);

        modeMgr->setText("模式设置");
        modeName->setText(changeModeData.modeName);
    }
    
    std::string imagePic =  changeModeData.imageDir;
    imagePic.erase(11, 8);
    modePic->setBackgroundResource(imagePic);
    temperature->setText(changeModeData.temperature + std::string("℃"));
    chuShuiLiang->setText(std::to_string(changeModeData.chuShuiLiang) + std::string("mL"));


    // changeModeData.dataType = nullptr;
    // changeModeData.imageDir = nullptr;
    // changeModeData.modeName = nullptr;
    // changeModeData.temperature = nullptr;
    // changeModeData.chuShuiLiang = -1;
    // changeModeData.canDelete = -1;
};

void PageModeAdd::loadPageInfo(){
    modeMgr = (TextView *)thisLayout->findViewById(demo::R::id::mode_mgr);
    home = (ImageView *)thisLayout->findViewById(demo::R::id::home);
    back = (ImageView *)thisLayout->findViewById(demo::R::id::back);
    modePic = (ViewGroup *)thisLayout->findViewById(demo::R::id::mode_pic);
    modeName = (TextView *)thisLayout->findViewById(demo::R::id::text2);
    temperature = (TextView *)thisLayout->findViewById(demo::R::id::text4);
    chuShuiLiang = (TextView *)thisLayout->findViewById(demo::R::id::text6);
    changePic = (ImageView *)thisLayout->findViewById(demo::R::id::mode_change_pic);
    tempJian = (ImageView *)thisLayout->findViewById(demo::R::id::jian1);
    tempJia = (ImageView *)thisLayout->findViewById(demo::R::id::jia1);
    chuShuiJian = (ImageView *)thisLayout->findViewById(demo::R::id::jian2);
    chuShuiJia = (ImageView *)thisLayout->findViewById(demo::R::id::jia2);
    submit = (ViewGroup *)thisLayout->findViewById(demo::R::id::mode_do_sure);
    deleteMode = (ImageView *)thisLayout->findViewById(demo::R::id::mode_delete);
}

void PageModeAdd::setOnClickListenerMethod(){
    mTools->setOnClickListenerToShowPage((ViewGroup *)back, PAGE_WATER_MODE);
    mTools->setOnClickListenerToShowPage((ViewGroup *)home, PAGE_MODE1);

    if (!changeModeData.canDelete)
    {
        deleteMode->setVisibility(View::GONE);
    }

    if (changeModeData.canDelete)
    {   
        /**
         * 是否删除
         */
        deleteMode->setOnClickListener([this](View &v){
            d_windMgr->showPopPage(POP_MODE_DELETE, [this](){
                d_fileLoad->deleteModeData(dataPos);
                d_windMgr->showPage(PAGE_WATER_MODE);
            }, nullptr, dataPos);
        });

        /**
        * 更换图片——修改
        */
        changePic->setOnClickListener([this](View &){
            changeModeData.imageDir.erase(11, 8);
            for (int i = 0; ; i++)
            {
                if (__imageDirs__.at(i) == changeModeData.imageDir)
                {
                    if (i == __imageDirs__.size() - 1)
                    {
                        std::string nImagePic =  __imageDirs__.at(0);
                        nImagePic.insert(11, "_chushui");
                        changeModeData.imageDir = nImagePic;
                        modePic->setBackgroundResource(__imageDirs__.at(0));
                        break;
                    }
                    std::string nImagePic =  __imageDirs__.at(i + 1);
                    nImagePic.insert(11, "_chushui");
                    changeModeData.imageDir = nImagePic;
                    modePic->setBackgroundResource(__imageDirs__.at(i + 1));
                    break;
                }
                if (i == __imageDirs__.size() - 1)
                {
                    i = 0;
                }
            }
        });

        /**
         * 加减温度
         */
        tempJian->setOnClickListener([this](View &){
            std::string temp = changeModeData.temperature;
            int nTemp = 0;
            if (!temp.empty())
            {
                nTemp = std::stoi(temp) - 1;
            }
            if (nTemp >= 45)
            {
                changeModeData.temperature = std::to_string(nTemp);
                temperature->setText(std::to_string(nTemp) + "℃");
            }
        });

        tempJia->setOnClickListener([this](View &){
            std::string temp = changeModeData.temperature;
            int nTemp = 0;
            if (!temp.empty())
            {
                nTemp = std::stoi(temp) + 1;
            }
            if (nTemp <= 98)
            {
                changeModeData.temperature = std::to_string(nTemp);
                temperature->setText(std::to_string(nTemp) + "℃");
            }
        });
    }

    /**
     * 出水量加减
     */
    chuShuiJian->setOnClickListener([this](View &){
        int liang = changeModeData.chuShuiLiang;
        int nLiang = liang - 50;
        if (nLiang >= 150)
        {
            changeModeData.chuShuiLiang = nLiang;
            chuShuiLiang->setText(std::to_string(nLiang) + "mL");
        }
    });
    chuShuiJia->setOnClickListener([this](View &){
        int liang = changeModeData.chuShuiLiang;
        int nLiang = liang + 50;
        if (nLiang <= 1000)
        {
            changeModeData.chuShuiLiang = nLiang;
            chuShuiLiang->setText(std::to_string(nLiang) + "mL");
        }
    });

    /**
     * 确定提交
     */
    submit->setOnClickListener([this](View &){
        // LOGE("changeModeData = %s", changeModeData.toString().c_str());
        // LOGE("dataPos = %d", dataPos);
        // LOGE("changeModeDatadataPos = %d", changeModeData.dataPos);
        // LOGE("changeModeDatadataType = %s", changeModeData.dataType.c_str());
        d_fileLoad->updateModeDataByPos(changeModeData, dataPos);
        d_windMgr->showPage(PAGE_WATER_MODE);
    });
}

// void PageModeAdd::setOnClickListenerMethodUpdate(){
//     mTools->setOnClickListenerToShowPage((ViewGroup *)back, PAGE_WATER_MODE);
//     mTools->setOnClickListenerToShowPage((ViewGroup *)home, PAGE_MODE1);

//     if (!changeModeData.canDelete)
//     {
//         deleteMode->setVisibility(View::GONE);
//     }

//     if (changeModeData.canDelete)
//     {   
//         /**
//         * 更换图片——修改
//         */
//         changePic->setOnClickListener([this](View &){
//             changeModeData.imageDir.erase(11, 8);
//             for (int i = 0; ; i++)
//             {
//                 if (__imageDirs__.at(i) == changeModeData.imageDir)
//                 {
//                     if (i == __imageDirs__.size() - 1)
//                     {
//                         std::string nImagePic =  __imageDirs__.at(0);
//                         nImagePic.insert(11, "_chushui");
//                         changeModeData.imageDir = nImagePic;
//                         modePic->setBackgroundResource(__imageDirs__.at(0));
//                         break;
//                     }
//                     std::string nImagePic =  __imageDirs__.at(i + 1);
//                     nImagePic.insert(11, "_chushui");
//                     changeModeData.imageDir = nImagePic;
//                     modePic->setBackgroundResource(__imageDirs__.at(i + 1));
//                     break;
//                 }
//                 if (i == __imageDirs__.size() - 1)
//                 {
//                     i = 0;
//                 }
//             }
//         });

//         /**
//          * 加减温度
//          */
//         tempJian->setOnClickListener([this](View &){
//             std::string temp = changeModeData.temperature;
//             int nTemp = 0;
//             if (!temp.empty())
//             {
//                 nTemp = std::stoi(temp) - 1;
//             }
//             if (nTemp >= 45)
//             {
//                 changeModeData.temperature = std::to_string(nTemp);
//                 temperature->setText(std::to_string(nTemp) + "℃");
//             }
//         });

//         tempJia->setOnClickListener([this](View &){
//             std::string temp = changeModeData.temperature;
//             int nTemp = 0;
//             if (!temp.empty())
//             {
//                 nTemp = std::stoi(temp) + 1;
//             }
//             if (nTemp <= 98)
//             {
//                 changeModeData.temperature = std::to_string(nTemp);
//                 temperature->setText(std::to_string(nTemp) + "℃");
//             }
//         });
//     }

//     /**
//      * 出水量加减
//      */
//     chuShuiJian->setOnClickListener([this](View &){
//         int liang = changeModeData.chuShuiLiang;
//         int nLiang = liang - 50;
//         if (nLiang >= 150)
//         {
//             changeModeData.chuShuiLiang = nLiang;
//             chuShuiLiang->setText(std::to_string(nLiang) + "mL");
//         }
//     });
//     chuShuiJia->setOnClickListener([this](View &){
//         int liang = changeModeData.chuShuiLiang;
//         int nLiang = liang + 50;
//         if (nLiang <= 1000)
//         {
//             changeModeData.chuShuiLiang = nLiang;
//             chuShuiLiang->setText(std::to_string(nLiang) + "mL");
//         }
//     });

//     /**
//      * 确定提交
//      */
//     submit->setOnClickListener([this](View &){
//         d_fileLoad->updateModeDataByPos(changeModeData, dataPos);
//         d_windMgr->showPage(PAGE_WATER_MODE);
//     });
// }

PageModeAdd::~PageModeAdd()
{
}